package y2016.date0901.pm.二维数组闭环替换;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author 问题来自
 * http://bbs.csdn.net/topics/391948572?page=1#post-401127140
 * 提问者：http://my.csdn.net/sozdream
 * 作者：http://my.csdn.net/bree06
 */
public class Bree {
    static int[][] box = {
            {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0},
            {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},// 这里有一个缺口
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
            {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
    //	    static int[][] box = new int[300][300]; // 测试300x300的最坏情况
    final static int x = box.length, y = box[0].length;

    @Test
    public void main() throws Exception {
        mark();
        print();
    }

    static void print() {
        for (int i = 0; i < x; i++) {
            System.out.println(Arrays.toString(box[i]));
        }
    }

    static void mark() {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if ((box[i][j] | 0) == 0) {
                    box[i][j] = 2;
                }
            }
        }
        out:
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (box[i][j] == 2) {
                    box[i][j] = 0;
                } else if (box[i][j] == 1) {
                    continue out;
                }
            }
        }
        out:
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (box[j][i] == 2) {
                    box[j][i] = 0;
                } else if (box[j][i] == 1) {
                    continue out;
                }
            }
        }
        out:
        for (int i = 0; i < x; i++) {
            for (int j = 1; j < y; j++) {
                if (box[i][y - j] == 2) {
                    box[i][y - j] = 0;
                } else if (box[i][y - j] == 1) {
                    continue out;
                }
            }
        }
        out:
        for (int i = 0; i < y; i++) {
            for (int j = 1; j < x; j++) {
                if (box[x - j][i] == 2) {
                    box[x - j][i] = 0;
                } else if (box[x - j][i] == 1) {
                    continue out;
                }
            }
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (box[i][j] == 2 && zero_right(i, j)) {
                    box[i][j] = 0;
                }
            }
        }
        for (int i = 0; i < x; i++) {
            for (int j = 1; j < y; j++) {
                if (box[i][x - j] == 2 && zero_right(i, x - j)) {
                    box[i][x - j] = 0;
                }
            }
        }
        for (int i = 1; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (box[x - i][j] == 2 && zero_right(x - i, j)) {
                    box[x - i][j] = 0;
                }
            }
        }
        for (int i = 1; i < x; i++) {
            for (int j = 1; j < y; j++) {
                if (box[x - i][x - j] == 2 && zero_right(x - i, x - j)) {
                    box[x - i][x - j] = 0;
                }
            }
        }
    }

    static boolean zero_right(int x, int y) {
        return (box[x - 1][y] == 0 || box[x][y - 1] == 0 || box[x][y + 1] == 0 || box[x + 1][y] == 0);
    }
}

